package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Department;
import com.example.demo.entity.WorkOrder;
import com.example.demo.service.IDepartmentService;
import com.example.demo.service.IWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class WorkOrderController {

    @Autowired
    private IWorkOrderService workOrderService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    // 保存工单
    @PostMapping("/save")
    public Object save(@Valid @RequestBody WorkOrder workOrder) {
        workOrder.setCreateTime(new Date());
        //工单编号不能重复
        QueryWrapper<WorkOrder> qw = new QueryWrapper<>();
        qw.eq("order_no", workOrder.getOrderNo());
        List<WorkOrder> orders = workOrderService.list(qw);
        if (orders.size() > 0) {
            return "工单编号重复";
        }
        return workOrderService.save(workOrder);
    }

    // 删除工单
    @PostMapping("/delete")
    public Object delete(@RequestParam Long id) {
        return workOrderService.removeById(id);
    }

    // 更新工单
    @PostMapping("/update")
    public Object update(@RequestBody WorkOrder workOrder) {
        //工单编号不能重复
        QueryWrapper<WorkOrder> qw = new QueryWrapper<>();
        qw.eq("order_no", workOrder.getOrderNo());
        List<WorkOrder> orders = workOrderService.list(qw);
        if (orders.size() > 0) {
            return "工单编号重复";
        }
        return workOrderService.updateById(workOrder);
    }

    // 查询工单（分页）
    @PostMapping("/search")
    public Object search(@RequestParam Integer current, @RequestParam Integer size) {
        return workOrderService.page(new Page<>(current, size));
    }

    // 分派工单
    @PostMapping("/fenpai")
    public Object fenpai(@RequestParam Long id, @RequestParam Long deptId, @RequestParam String deptName) {
        Department dept = departmentService.getById(deptId);
        if (dept == null) {
            return deptName + "不存在";
        }

        WorkOrder workOrder = new WorkOrder();
        workOrder.setId(id);
        workOrder.setHandleDeptId(deptId);
        workOrder.setFenpaiTime(new Date());
        return workOrderService.updateById(workOrder);
    }


    /**
     * @param type 1=查询7月每天的工单总量、超期率
     *             type 2=查询7月每个部门的工单总量、超期率
     *             type 3=查询7月每个工单类型的工单总量、超期率
     * @return
     */
    @PostMapping("/selectNums")
    public Object selectNums(@RequestParam Integer type,@RequestParam String month) {
        switch (type) {
            case 1:
                return workOrderService.getDailyOrderStatsByMonth(month);
            case 2:
                return workOrderService.getDeptOrderStatsByMonth(month);
            case 3:
                return workOrderService.getTypeOrderStatsByMonth(month);
        }
        return "type=" + type + "无效";
    }


    /**
     * 测试存储redis
     * @return
     */
    @RequestMapping("/set")
    public String setRedis() {
        redisTemplate.opsForValue().set("name", "Hello World！");
        return "redis 存储成功！";
    }


    /**
     * 测试取redis
     * @return
     */
    @RequestMapping("/get")
    public String getRedis() {
        //通过 key 值读取 value
        String result = redisTemplate.opsForValue().get("name");
        return result;
    }
}
